package api

import (
	"errors"
	"log"
	"net/http"

	"gitee.com/Yu-eng/Myproject/my-go-blog/common"
	"gitee.com/Yu-eng/Myproject/my-go-blog/models"
	"gitee.com/Yu-eng/Myproject/my-go-blog/utils"
)

func (*Api) SaveAndUpdatePost(w http.ResponseWriter, r *http.Request) {
	//这里需要获得操作文章的用户的信息
	//并判断用户是否登录
	//登录后 传入header数据内包含authorization 信息 验证是否合法 判断是否登录
	token := r.Header.Get("Authorization")
	_, claim, err := utils.ParseToken(token)
	if err != nil {
		log.Println("获取用户登录Authorization 失败,", err)
		common.Error(w, errors.New("登录信息已经过期,请重新登录！"))
		return
	}
	uid := claim.Uid //这里拿到了用户的登录id

	//POST请求 操作代表Sava操作
	method := r.Method
	switch method {
	case http.MethodPost:
		//对应文章发布的操作
		params := common.GetRequestJsonParam(r)
		cId := params["categoryId"].(string) //前端返回的数据是string
		content := params["content"].(string)
		markDown := params["markdown"].(string)
		slug := params["slug"].(string)
		title := params["title"].(string)
		postType := params["type"].(string)
		post := &models.Post{
			Pid:        -1,
			Title:      title,
			Slug:       slug,
			Content:    content,
			Markdown:   markDown,
			CategoryId: cId,
		}
	case http.MethodPut:
		//对应update操作
	}

}
